package com.online_mall.shoppingCart.service.impl;

import com.online_mall.commodity.dao.CommodityDao;
import com.online_mall.commodity.entity.Commodity;
import com.online_mall.orders.dao.OrdersDao;
import com.online_mall.orders.entity.Orders;
import com.online_mall.shoppingCart.dao.ShoppingCartDao;
import com.online_mall.shoppingCart.entity.ShoppingCart;
import com.online_mall.shoppingCart.service.ShoppingCartService;
import com.online_mall.uitl.ResultFormat;
import com.online_mall.uitl.ResultUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * (ShoppingCart)表服务实现类
 *
 * @author makejava
 * @since 2025-09-02 15:03:38
 */
@Service("shoppingCartService")
@Transactional(rollbackFor = Exception.class)
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Resource
    private ShoppingCartDao shoppingCartDao;
    @Resource
    private CommodityDao commodityDao;
    @Resource
    private OrdersDao ordersDao;


    @Override
    public ResultFormat queryAll(ShoppingCart shoppingCart) {
        return ResultUtil.success(shoppingCartDao.queryAll(shoppingCart));
    }

    @Override
    public ResultFormat insert(ShoppingCart shoppingCart) {
        if(shoppingCartDao.queryAll(shoppingCart).size() != 0){
            return ResultUtil.error(-1,"该商品在购物车已存在，请勿重复添加");
        }
        Commodity commodity = new Commodity();
        commodity.setCommodityId(shoppingCart.getCommodityId());
        commodity.setNumber(shoppingCart.getNumber());
        commodityDao.addShopping(commodity);
        return ResultUtil.success(shoppingCartDao.insert(shoppingCart));
    }

    @Override
    public ResultFormat update(ShoppingCart shoppingCart) {
        ShoppingCart shoppingCart1 = shoppingCartDao.queryAll(shoppingCart).get(0);
        Commodity commodity = new Commodity();
        commodity.setCommodityId(shoppingCart.getCommodityId());
        commodity.setNumber(shoppingCart.getNumber() - shoppingCart1.getNumber());
        commodityDao.addShopping(commodity);
        return ResultUtil.success(shoppingCartDao.update(shoppingCart));
    }

    @Override
    public ResultFormat delete(ShoppingCart shoppingCart) {
        Commodity commodity = new Commodity();
        commodity.setCommodityId(shoppingCart.getCommodityId());
        commodity.setNumber(shoppingCart.getNumber());
        commodityDao.deleteShopping(commodity);
        return ResultUtil.success(shoppingCartDao.delete(shoppingCart));
    }

    @Override
    public ResultFormat buy(ShoppingCart shoppingCart) {
        Orders orders = new Orders();
        orders.setOrdersId("订单"+System.currentTimeMillis());
        orders.setUserId(shoppingCart.getUserId());
        orders.setCommodityName(shoppingCart.getCommodityName());
        orders.setNumber(shoppingCart.getNumber());
        orders.setPrice(shoppingCart.getPrice());
        orders.setTotal(shoppingCart.getPrice() * shoppingCart.getNumber());
        orders.setCommodityId(shoppingCart.getCommodityId());
        ordersDao.insert(orders);
        return ResultUtil.success(shoppingCartDao.delete(shoppingCart));
    }
}
